<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {
	public function __construct(){
        parent::__construct();
      }


	public function index()
	{

	  $this->load->view("login");
	}



	public function login_check(){
       $username=$this->input->post("txtUser");
       $passwd=$this->input->post("txtPwd");

       if(!empty($username)&& !empty($passwd)){
        $sql="select * from `account` where `username`='$username'";
			
          $result=$this->db->query($sql);
          $row=$result->row();
        if($row->passwd == $passwd){
          $arr=array("realname"=>$row->realname,"username"=>$row->username,"userid"=>$row->id,"depart"=>$row->depart,"roleid"=>$row->role_id);
          $this->session->set_userdata($arr);
					//echo "success";
        redirect("index.php/home");
        }else{
           	//echo "fail";
          redirect("index.php/login");

        }
      }else{
					//echo "fail";
        redirect("index.php/login");

      }
	}

  public function login_out(){

		         if($this->session->sess_destroy())
	                     {
	                     redirect("index.php/login");
	                     }else{
	                      redirect("index.php/login");
	                     }

	}

  public function changepasswd(){
    $oldpass=$this->input->get("oldpass");
    $newpass=$this->input->get("newpass");
    $userid=$this->session->userdata("userid");
                $sql="select passwd from account where id='$userid' and passwd='$oldpass'";
                $query=$this->db->query($sql);
                $num=$query->num_rows();
                if($num>0){
    $sql="update account set passwd='$newpass' where id='$userid'";
    if($this->db->query($sql)){
       echo "密码修改成功,新密码是：".$newpass."!";
    }else{
       echo "哎呀！密码修改失败,系统出现故障了！";
    }
        }else{
                echo "输入的旧密码错误，错误密码是：".$oldpass."!";
        }


  }

}
